package com.songlanyun.modules.opus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.modules.opus.entity.OpusTheme;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.opus.dao.OpusGoodsDao;
import com.songlanyun.modules.opus.entity.OpusGoods;
import com.songlanyun.modules.opus.service.OpusGoodsService;


@Service("opusGoodsService")
public class OpusGoodsServiceImpl extends ServiceImpl<OpusGoodsDao, OpusGoods> implements OpusGoodsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OpusGoods> page = this.page(
                new Query<OpusGoods>().getPage(params),
                new QueryWrapper<OpusGoods>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveOpusGoods(Long oid, List<Long> goodsIds) {
        goodsIds.forEach(g -> {
            OpusGoods opusGoods = new OpusGoods();
            opusGoods.setOpusId(oid);
            opusGoods.setGoodsId(g);
            this.baseMapper.insert(opusGoods);
        });
    }

    @Override
    public void removeByOid(Long oid) {
        this.baseMapper.delete(
                new LambdaQueryWrapper<OpusGoods>()
                        .eq(OpusGoods::getOpusId, oid)
        );
    }

    @Override
    public List<OpusGoods> listByOpusId(Long oid) {
        List<OpusGoods> opusGoodsList = this.baseMapper.selectList(
                new LambdaQueryWrapper<OpusGoods>()
                        .eq(OpusGoods::getOpusId, oid)
        );

        if (opusGoodsList != null) {
            opusGoodsList.forEach(OpusGoods::loadGoods);
        }

        return opusGoodsList;
    }

}